package top.wu2020.lazy.ws.chat.application.impl;

import org.wu.framework.database.lazy.web.plus.stereotype.LazyApplication;
import top.wu2020.lazy.ws.chat.application.CommunityApplication;
import org.springframework.web.bind.annotation.*;
import org.wu.framework.web.response.Result;
import org.wu.framework.web.response.ResultFactory;
import org.springframework.beans.factory.annotation.Autowired;
import top.wu2020.lazy.ws.chat.domain.model.community.Community;
import top.wu2020.lazy.ws.chat.application.command.community.CommunityRemoveCommand;
import top.wu2020.lazy.ws.chat.application.command.community.CommunityStoryCommand;
import top.wu2020.lazy.ws.chat.application.command.community.CommunityUpdateCommand;
import top.wu2020.lazy.ws.chat.application.command.community.CommunityQueryListCommand;
import top.wu2020.lazy.ws.chat.application.command.community.CommunityQueryOneCommand;
import top.wu2020.lazy.ws.chat.application.assembler.CommunityDTOAssembler;
import top.wu2020.lazy.ws.chat.application.dto.CommunityDTO;
import java.util.stream.Collectors;
import jakarta.annotation.Resource;
import top.wu2020.lazy.ws.chat.domain.model.community.CommunityRepository;
import java.util.List;
import org.wu.framework.lazy.orm.database.lambda.domain.LazyPage;
/**
 * describe community 
 *
 * @author Jia wei Wu
 * @date 2024/05/24 08:52 晚上
 * @see org.wu.framework.lazy.orm.core.persistence.reverse.lazy.ddd.DefaultDDDLazyApplicationImpl 
 **/
@LazyApplication
public class CommunityApplicationImpl implements CommunityApplication {

    @Resource
    CommunityRepository communityRepository;
    /**
     * describe 新增
     *
     * @param communityStoryCommand 新增     
     * @return {@link Result<Community>} 新增后领域对象     
     
     * @author Jia wei Wu
     * @date 2024/05/24 08:52 晚上
     **/

    @Override
    public Result<Community> story(CommunityStoryCommand communityStoryCommand) {
        Community community = CommunityDTOAssembler.INSTANCE.toCommunity(communityStoryCommand);
        return communityRepository.story(community);
    }
    /**
     * describe 批量新增
     *
     * @param communityStoryCommandList 批量新增     
     * @return {@link Result<List<Community>>} 新增后领域对象集合     
     
     * @author Jia wei Wu
     * @date 2024/05/24 08:52 晚上
     **/

    @Override
    public Result<List<Community>> batchStory(List<CommunityStoryCommand> communityStoryCommandList) {
        List<Community> communityList = communityStoryCommandList.stream().map( CommunityDTOAssembler.INSTANCE::toCommunity).collect(Collectors.toList());
        return communityRepository.batchStory(communityList);
    }
    /**
     * describe 更新
     *
     * @param communityUpdateCommand 更新     
     * @return {@link Result<Community>} 领域对象     
     
     * @author Jia wei Wu
     * @date 2024/05/24 08:52 晚上
     **/

    @Override
    public Result<Community> updateOne(CommunityUpdateCommand communityUpdateCommand) {
        Community community = CommunityDTOAssembler.INSTANCE.toCommunity(communityUpdateCommand);
        return communityRepository.story(community);
    }

    /**
     * describe 查询单个
     *
     * @param communityQueryOneCommand 查询单个     
     * @return {@link Result<CommunityDTO>} DTO对象     
     
     * @author Jia wei Wu
     * @date 2024/05/24 08:52 晚上
     **/

    @Override
    public Result<CommunityDTO> findOne(CommunityQueryOneCommand communityQueryOneCommand) {
        Community community = CommunityDTOAssembler.INSTANCE.toCommunity(communityQueryOneCommand);
        return communityRepository.findOne(community).convert(CommunityDTOAssembler.INSTANCE::fromCommunity);
    }

    /**
     * describe 查询多个
     *
     * @param communityQueryListCommand 查询多个     
     * @return {@link Result<List<CommunityDTO>>} DTO对象     
     
     * @author Jia wei Wu
     * @date 2024/05/24 08:52 晚上
     **/

    @Override
    public Result<List<CommunityDTO>> findList(CommunityQueryListCommand communityQueryListCommand) {
        Community community = CommunityDTOAssembler.INSTANCE.toCommunity(communityQueryListCommand);
        return communityRepository.findList(community)        .convert(communitys -> communitys.stream().map(CommunityDTOAssembler.INSTANCE::fromCommunity).collect(Collectors.toList())) ;
    }

    /**
     * describe 分页查询多个
     *
     * @param communityQueryListCommand 分页查询多个     
     * @return {@link Result<LazyPage<CommunityDTO>>} 分页DTO对象     
     
     * @author Jia wei Wu
     * @date 2024/05/24 08:52 晚上
     **/

    @Override
    public Result<LazyPage<CommunityDTO>> findPage(int size,int current,CommunityQueryListCommand communityQueryListCommand) {
        Community community = CommunityDTOAssembler.INSTANCE.toCommunity(communityQueryListCommand);
        return communityRepository.findPage(size,current,community)        .convert(page -> page.convert(CommunityDTOAssembler.INSTANCE::fromCommunity))            ;
    }

    /**
     * describe 删除
     *
     * @param communityRemoveCommand 删除     
     * @return {@link Result<Community>}      
     
     * @author Jia wei Wu
     * @date 2024/05/24 08:52 晚上
     **/

    @Override
    public Result<Community> remove(CommunityRemoveCommand communityRemoveCommand) {
     Community community = CommunityDTOAssembler.INSTANCE.toCommunity(communityRemoveCommand);
     return communityRepository.remove(community);
    }

}